package dao;

import entity.Calendario;
import entity.IEntity;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import vo.IValueObject;

/**
 *
 * @author Alexander
 */
public class CalendarioDAO implements IDAO{

    @Override
    public void persist(IEntity entity, EntityManager em) {
        em.persist(entity);
    }

    @Override
    public IValueObject find(Object id, EntityManager em) {
        Calendario cal = (Calendario)em.find(Calendario.class, (int) id);
        if (cal!=null) {
            return cal.toVO();
        } else {
            return null;
        }
    }

    @Override
    public void update(IEntity entity, EntityManager em) {
        em.merge(entity);
    }

    @Override
    public void delete(Object id, EntityManager em) {
        Calendario cal = (Calendario)em.getReference(Calendario.class, (int) id);
        em.remove(cal);
    }

    @Override
    public List<IValueObject> getList(EntityManager em) {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Calendario.class));
        Query q = em.createQuery(cq);
        return q.getResultList();
    }
    
}
